<script>
import debounce from 'lodash/debounce';
import { RadioGroup } from '@components/Form/Radio';
import { LabeledInput } from '@components/Form/LabeledInput';
import LabeledSelect from '@shell/components/form/LabeledSelect';
import ShellInput from '@shell/components/form/ShellInput';
import { _VIEW } from '@shell/config/query-params';
import { isEmpty } from '@shell/utils/object';

export default {
  emits: ['update:value'],

  props: {
    mode: {
      type:     String,
      required: true,
    },
    value: {
      type:    Object,
      default: () => {
        return {};
      }
    }
  },

  components: {
    RadioGroup, LabeledInput, LabeledSelect, ShellInput
  },

  data() {
    return {
      selectHook:     null,
      defaultExec:    { exec: { command: [] } },
      defaultHttpGet: {
        httpGet: {
          host:        '',
          path:        '',
          port:        null,
          scheme:      '',
          httpHeaders: null
        }
      },
      schemeOptions: ['HTTP', 'HTTPS']
    };
  },

  computed: {
    isView() {
      return this.mode === _VIEW;
    },
  },

  created() {
    if (this.value) {
      this.selectHook = Object.keys(this.value)[0];
    }

    if (isEmpty(this.value)) {
      this.selectHook = 'none';
    }

    this.queueUpdate = debounce(this.update, 500);
  },

  methods: {
    addHeader() {
      const header = { name: '', value: '' };

      if (!this.value.httpGet.httpHeaders) {
        this.value.httpGet['httpHeaders'] = [];
      }

      this.value.httpGet.httpHeaders.push(header);
    },

    removeHeader(index) {
      this.value.httpGet.httpHeaders.splice(index, 1);
    },

    update() {
      const { ...leftovers } = this.value;

      switch (this.selectHook) {
      case 'none':
        this.deleteLeftovers(leftovers);
        break;
      case 'exec':
        this.deleteLeftovers(leftovers);
        Object.assign(this.value, this.defaultExec);
        break;
      case 'httpGet':
        this.deleteLeftovers(leftovers);
        Object.assign(this.value, this.defaultHttpGet);
        break;
      default:
        break;
      }

      this.$emit('update:value', this.value);
    },

    deleteLeftovers(leftovers) {
      if (leftovers) {
        for (const obj in leftovers) {
          delete this.value[obj];
        }
      }
    }
  }
};
</script>

<template>
  <div>
    <div class="row mb-10">
      <RadioGroup
        v-model:value="selectHook"
        name="selectHook"
        :options="['none', 'exec', 'httpGet']"
        :labels="[
          t('generic.none'),
          t('workload.container.lifecycleHook.exec.add'),
          t('workload.container.lifecycleHook.httpGet.add'),
        ]"
        :mode="mode"
        @update:value="update"
      />
    </div>

    <template v-if="selectHook === 'exec'">
      <div class="mb-20 single-value">
        <h4>{{ t('workload.container.lifecycleHook.exec.title') }}</h4>
        <div>
          <ShellInput
            v-model:value="value.exec.command"
            :mode="mode"
            :label="t('workload.container.lifecycleHook.exec.command.label')"
            :placeholder="t('workload.container.lifecycleHook.exec.command.placeholder', null, true)"
            required
          />
        </div>
      </div>
    </template>

    <template v-if="selectHook === 'httpGet'">
      <h4>{{ t('workload.container.lifecycleHook.httpGet.title') }}</h4>
      <div class="var-row">
        <LabeledInput
          v-model:value="value.httpGet.host"
          :label="t('workload.container.lifecycleHook.httpGet.host.label')"
          :placeholder="t('workload.container.lifecycleHook.httpGet.host.placeholder')"
          :mode="mode"
          @update:value="update"
        />
        <LabeledInput
          v-model:value="value.httpGet.path"
          :label="t('workload.container.lifecycleHook.httpGet.path.label')"
          :placeholder="t('workload.container.lifecycleHook.httpGet.path.placeholder')"
          :mode="mode"
          @update:value="update"
        />
        <LabeledInput
          v-model:value.number="value.httpGet.port"
          type="number"
          :label="t('workload.container.lifecycleHook.httpGet.port.label')"
          :placeholder="t('workload.container.lifecycleHook.httpGet.port.placeholder')"
          :mode="mode"
          required
          @update:value="update"
        />
        <LabeledSelect
          v-model:value="value.httpGet.scheme"
          :label="t('workload.container.lifecycleHook.httpGet.scheme.label')"
          :placeholder="t('workload.container.lifecycleHook.httpGet.scheme.placeholder')"
          :options="schemeOptions"
          :mode="mode"
          @update:value="update"
        />
      </div>

      <h4>{{ t('workload.container.lifecycleHook.httpHeaders.title') }}</h4>
      <div
        v-for="(header, index) in value.httpGet.httpHeaders"
        :key="index"
        class="var-row"
        data-testid="hookoption-header-row"
      >
        <LabeledInput
          v-model:value="value.httpGet.httpHeaders[index].name"
          :label="t('workload.container.lifecycleHook.httpHeaders.name.label')"
          :placeholder="t('workload.container.lifecycleHook.httpHeaders.name.placeholder')"
          class="single-value"
          :mode="mode"
          required
          @update:value="update"
        />
        <LabeledInput
          v-model:value="value.httpGet.httpHeaders[index].value"
          :label="t('workload.container.lifecycleHook.httpHeaders.value.label')"
          :placeholder="t('workload.container.lifecycleHook.httpHeaders.value.placeholder')"
          class="single-value"
          :mode="mode"
          @update:value="update"
        />
        <div class="remove">
          <button
            v-if="!isView"
            type="button"
            class="btn role-link ml0"
            :disabled="mode==='view'"
            @click.stop="removeHeader(index)"
          >
            <t k="generic.remove" />
          </button>
        </div>
      </div>

      <div>
        <button
          v-if="!isView"
          type="button"
          class="btn role-link mb-20"
          :disabled="mode === 'view'"
          data-testid="hookoption-add-header-button"
          @click.stop="addHeader"
        >
          Add Header
        </button>
      </div>
    </template>
  </div>
</template>

<style lang='scss' scoped>
.var-row{
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr 100px;
  grid-column-gap: 20px;
  margin-bottom: 20px;
  align-items: center;

  .single-value {
    grid-column: span 2;
  }

  .labeled-select {
    min-height: $input-height;
  }
  .remove BUTTON {
    padding: 0px;
  }
}
</style>
